package cn.tedu.mimall.product.service.impl;

import cn.tedu.mimall.product.ex.ServiceException;
import cn.tedu.mimall.product.mapper.ClassifyMapper;
import cn.tedu.mimall.product.pojo.dto.ClassifyAddNewDTO;
import cn.tedu.mimall.product.pojo.dto.ClassifyUpdateDTO;
import cn.tedu.mimall.product.pojo.entity.Classify;
import cn.tedu.mimall.product.pojo.vo.ClassifyListItemVO;
import cn.tedu.mimall.product.pojo.vo.ClassifyStandardVO;
import cn.tedu.mimall.product.service.IClassifyService;
import cn.tedu.mimall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ClassifyServiceImpl implements IClassifyService {

    @Autowired
    ClassifyMapper classifyMapper;

    @Override
    public void insertAdd(ClassifyAddNewDTO classifyAddNewDTO) {
        log.debug("开始处理【添加分类】的业务，参数：{}",classifyAddNewDTO);
        int count = classifyMapper.countByName(classifyAddNewDTO.getName());
        if(count!=0){
            String message="添加分类失败，分类名已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Classify classify=new Classify();
        BeanUtils.copyProperties(classifyAddNewDTO,classify);
        int row = classifyMapper.insert(classify);
        if(row!=1){
            String message="添加分类失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除分类】的业务，参数：{}",id);
        int count = classifyMapper.countById(id);
        if(count==0){
            String message="删除分类失败，分类不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = classifyMapper.deleteById(id);
        if(row!=1){
            String message="删除分类失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void update(Long id, ClassifyUpdateDTO classifyUpdateDTO) {
        log.debug("开始处理【修改分类】的业务，参数：{}，{}",id,classifyUpdateDTO);
        ClassifyStandardVO query = classifyMapper.getStandardById(id);
        if(query==null){
            String message="修改分类失败，分类不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(query.getName().equals(classifyUpdateDTO.getName())){
            String message="修改分类失败，预修改的分类名与原分类名相同";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Classify classify=new Classify();
        BeanUtils.copyProperties(classifyUpdateDTO,classify);
        classify.setId(id);
        int row = classifyMapper.updateById(classify);
        if(row!=1){
            String message="修改分类失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public ClassifyStandardVO getById(Long id) {
        log.debug("开始处理【查询分类】的业务，参数：{}",id);
        ClassifyStandardVO result = classifyMapper.getStandardById(id);
        if(result==null){
            String message="查询分类数据失败，分类数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return result;
    }

    @Override
    public List<ClassifyListItemVO> list() {
        log.debug("开始处理【查询分类列表】的业务");
        return classifyMapper.list();
    }
}
